import {platformMap}  from '@/common/models.js'

const authMixin = {
  methods: {
    login(platfrom, accessToken) {
      console.log('get', platformMap.get(platfrom))
      const that = this
      if (accessToken) {
        let requestData = {
          vid: that.vid,
          mid: that.mid,
        }
          
        if (platfrom == 'tiktok') {
          requestData.code = accessToken
          requestData['redirect_uri'] = that.loginState
        } else {
          requestData.token = accessToken
        }
        if (uni.getStorageSync('userFrom')) {
          requestData.source = uni.getStorageSync('userFrom')
        }
        if (uni.getStorageSync('guestId')) {
          requestData['guest_id'] = uni.getStorageSync('guestId')
        }
        uni.request({
          url: this.configs.apiUrl + '/api/user/' + platfrom,
          data: requestData,
          success(res) {
            that.handleLoginSuccess(res, platfrom);
          }
        })
      }
    },
    handleLoginSuccess(res, platfrom, isByCode) {
      const that = this
      if (res.data.code === 1) {
        uni.showToast({
          title: that.$t('login.success'),
          icon: "success"
        });
        var avatar = '';
        if (res.data.data.userinfo.avatar) {
          var str = res.data.data.userinfo.avatar;
          if (str.indexOf("data:image") == -1) {
            if (str.indexOf("http") != -1) {
              avatar = res.data.data.userinfo.avatar;
            } else {
              avatar = that.configs.imgUrl + res.data.data.userinfo.avatar;
            }
          }
        }
        uni.setStorage({ //快取配置資訊
          key: 'avatar',
          data: avatar
        })
        uni.setStorage({ //快取配置資訊
          key: 'userinfo',
          data: res.data.data.userinfo
        })
        uni.setStorage({
          key: 'userLoginType',
          data: res.data.data['login_type']
        })
        uni.setStorage({ //快取配置資訊
          key: 'config',
          data: res.data.data.config
        })

        // 1,fb;2,gg;3,titkok,4,account
        that.GAHandle('login', platformMap.get(platfrom), {
          'code': platfrom,
          mid: that.mid,
          vid: that.vid
        })


        const loginRedirectUri = uni.getStorageSync('loginRedirectUri') || ''
        uni.removeStorageSync('loginRedirectUri')
        if (loginRedirectUri != '') {
            uni.reLaunch({
              url: decodeURIComponent(loginRedirectUri)
            })
            return false;
        }
        if (that.reURL) {
          location.href = that.reURL
        } else if (that.vid) {
          uni.reLaunch({
            url: `/pages/client/tuan/detail?vid=${that.vid}&mid=${that.mid}`
          })
        } else if (platfrom == 'google' && that.loginState != 'loginpage') {
          uni.reLaunch({
            url: decodeURIComponent(that.loginState)
          })
        } else {
          const pages = getCurrentPages()
          // console.log({pages}, pages.length)
          if (pages.length == 1) {
            uni.reLaunch({
              url: '/pages/client/index'
            })
          } else {
            uni.navigateBack()
          }
        }
      } else {
        
        uni.showToast({
          title: that.$t('login.failed'),
          icon: "none",
          success() {
            uni.redirectTo({
              url: '/pages/login/login'
            })
          }
        });
      }
    },
  }
}

export default authMixin;
